const mongoose = require('mongoose');

// 获取

const get = async () => {
  const data = await mongoose.model("indent").find()
    .populate('usersNameId')
    .populate("movieId")
    .populate("cinemasId")
    .populate("theatersId")
    .populate("schedulesId")
    .populate('seatingId')
    .populate('seatsId')
    .sort({
      _id: -1
    })
    .exec()
  return data
}
//获取用户
const usrse = async () => await mongoose.model("users").find()
// 获取放映厅
const find = () => mongoose.model("theaters").find().populate("cinemasId").exec()
// 获取影院
let cinemas = async ({
  curPage,
  eachPage
}) => {
  const result = {
    curPage: ~~curPage,
    eachPage: ~~eachPage,
    total: 0,
    maxPage: 0,
    rows: [],
  }
  let cinemasMoel = mongoose.model("cinemas");
  // 获取总条数
  result.total = await cinemasMoel.countDocuments();
  result.maxPage = Math.ceil(result.total / result.eachPage);
  result.rows = await cinemasMoel
    // 操作类型
    .find()
    // 填充
    .populate("gradeId")
    // 忽略条数
    .skip((result.curPage - 1) * result.eachPage)
    // 查询条数
    .limit(result.eachPage)
    .sort({
      _id: -1
    })
    // 执行
    .exec();
  return result;
}


// 订单分页

const indentCur = async ({ curPage, eachPage }) => {
  // await mongoose.model('students').find();
  const result = {
    curPage: ~~curPage,
    eachPage: ~~eachPage,
    total: 0,
    maxPage: 0,
    rows: [],
  }
  const indentCurModel = mongoose.model('indent');
  //获取总条数
  result.total = await indentCurModel.countDocuments();
  result.maxPage = Math.ceil(result.total / result.eachPage);
  result.rows = await indentCurModel.find()
    .skip((result.curPage - 1) * result.eachPage)
    .limit(result.eachPage)
    .exec();

  return result
}

// 获取排片数据
const getSc = async () => {
  return await mongoose.model('schedules').find();
}

// 新增订单数据
const addIndent = async (data) => {
  const seatsArr = data.seatingId.split(",")
  let i = 0;

  const arr = []
  a()
  async function a() {
    // console.log(seatsArr);
    if (arr.length != seatsArr.length) {
      arr.push(await mongoose.model('seats').findOne({ _id: seatsArr[i] }))
      i++
      a()
    } else {
      data.seatsArr = JSON.stringify(arr);
      data.seatingId = seatsArr[0];


      await mongoose.model('seating').create({
        status: true,
        seating: JSON.stringify(arr)
      })


      return await mongoose.model('indent').create(data)
    }

  }

  // return await mongoose.model('indent').create(data)
}


// 新增订单选座数据
const addSeating = async ({ rowsId, status }) => {
  return await mongoose.model('indent').create({ seating: rowsId, status })
}
// //查询订单数据
let findOne = ({
  name
}) => mongoose.model("users").find({
  userName: name
})
let getIall = async ({
  usersNameId
}) => {
  return await mongoose.model("indent").find({
    usersNameId
  }).populate("usersNameId")
    .populate("cinemasId")
    .populate("theatersId")
    .populate('movieId')
    .populate('schedulesId')
    .populate('seatingId')
    .sort({
      _id: -1
    })
    .exec();
}
module.exports = {
  get,
  addIndent,
  find,
  cinemas,
  getSc,
  usrse,
  findOne,
  getIall,
  addSeating,
  indentCur
}